package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.sfrobot.entity.AssessTable;
import com.lizhi.yun.db.sfrobot.entity.UpDataListPojo;
import com.lizhi.yun.db.sfrobot.repository.AssessTableRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/12/15 11:32
 */
@Slf4j
@Service
public class RecvAssessTableHandler {

    @Resource
    private AssessTableRepo assessTableRepo;

    public String recvAssessTable(String json){
        UpDataPOJO upClientPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upClientPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*上传的数据信息信息*/
        UpDataListPojo upDataListPojo = SamJsonUtil.toObject(upClientPOJO.getData(), UpDataListPojo.class);
        List<AssessTable> assessTables = upDataListPojo.getAssessTables();
        if (assessTables.size() < 1){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        for (AssessTable assessTable : assessTables){
            /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
            assessTable.setCommUpTime(new Date());
            assessTable.setCreateTime(DateUtils.getNowDate());
            assessTable.setHospitalId(rpcClientPOJO.getOrganId());
            assessTable.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
            /*查询是否存在*/
            AssessTable example = new AssessTable();
            example.setId(assessTable.id);
            example.setHospitalId(rpcClientPOJO.getOrganId());
            example.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
            AssessTable recordFromDb = assessTableRepo.findOne(Example.of(example)).orElse(null);
            if (StringUtils.isEmpty(recordFromDb)){
                assessTable.setLid(StringUtils.newId());
            } else {
                assessTable.setLid(recordFromDb.lid);
            }
            log.info("上传的数据信息是assessTable:{}", assessTable);
            assessTableRepo.save(assessTable);
        }

        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
